package com.freely.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class HttpUtil {
	
	public static JSONObject parseJSON(String failureMessage) throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		InputStream input = null;
		BufferedReader reader = null;
		StringBuilder data = new StringBuilder();
		try {
			input = request.getInputStream();
			String temp = "";
			reader = new BufferedReader(new InputStreamReader(input,"utf-8"));
			while((temp = reader.readLine()) != null) {
				data.append(temp);
			}
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			if(reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		String result = data.toString();
		if(result.length() == 0 || result.trim().length() == 0) {
			ResponseMessageUtil.response(false, failureMessage);
			return null;
		}
		return JSON.parseObject(result);
		
	}
	
	public static JSONObject parseJSON(String failureMessage,Class...classes) throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		InputStream input = null;
		BufferedReader reader = null;
		StringBuilder data = new StringBuilder();
		try {
			input = request.getInputStream();
			String temp = "";
			reader = new BufferedReader(new InputStreamReader(input,"utf-8"));
			while((temp = reader.readLine()) != null) {
				data.append(temp);
			}
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			if(reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		String result = data.toString();
		if(result.length() == 0 || result.trim().length() == 0) {
			ResponseMessageUtil.response(false, failureMessage);
			return null;
		}
		JSONObject obj = JSON.parseObject(result);
		List<String> list = new ArrayList<>();
		for(Class temp : classes) {
			Field[] fields = temp.getDeclaredFields();
			for(Field key : fields) {
				list.add(key.getName());
			}
		}
		for(Entry<String,Object> entry : obj.entrySet()) {
			if(!list.contains(entry.getKey())) {
				ResponseMessageUtil.response(false, failureMessage);
				return null;
			}
		}
		return obj;
	}

	public static void responseResult(String json) throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().print(json);
	}
	
	
}
